﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ManWebRole.Models
{
    public class SpecificationModel
    {
        private SkySRMDBDataDataContext db = new SkySRMDBDataDataContext();

        public IEnumerable<Specification> GetAllSpecifications()
        {
            return db.Specifications;
        }

        public Specification GetSpecificationById(int id)
        {
            return db.Specifications.SingleOrDefault(s => s.Id == id);
        }

        public void AddSpecification(Specification specification)
        {
            db.Specifications.InsertOnSubmit(specification);
            db.SubmitChanges();
        }

        public void DeleteSpecification(int id)
        {
            Specification specification = GetSpecificationById(id);
            db.Specifications.DeleteOnSubmit(specification);
            db.SubmitChanges();
        }

        public void AddNewSpecificationOnProjectCreated(Project project)
        {
            Specification[] specifications = new Specification[3];
            for (int i = 0; i < specifications.Length; i++)
            {
                specifications[i] = new Specification();
                specifications[i].Type = (byte)i;
                specifications[i].StartTime = DateTime.Now;
                specifications[i].ProjectId = project.Id;
                specifications[i].Status = (byte)Status.Blank;
            }
            specifications[0].Name = project.Name + " - Vision & Scope Specification";
            specifications[1].Name = project.Name + " - User Requirements Specification";
            specifications[2].Name = project.Name + " - Software Requirements Specification";
            
            foreach (var item in specifications)
            {
                AddSpecification(item);
            }
            db.SubmitChanges();
        }
    }
}